﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TCHSHue.BLL;
using System.IO;

namespace TCHSHue.Modules.IndexControl.TinTuc
{
    public partial class ctrTinTuc : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                var strIdTheLoai = Request.QueryString["idTL"];
                int temp;
                int pCurrent = getPageCurrent();
                if (int.TryParse(strIdTheLoai, out temp))
                {
                    int idTheLoai = int.Parse(strIdTheLoai);
                    if (idTheLoai == Hang.THONGBAO)
                        lbNhanTrang.Text = "THÔNG BÁO";
                    else if (idTheLoai == Hang.TINTUC)
                        lbNhanTrang.Text = "TIN TỨC - SỰ KIỆN";
                    NapDanhSachTinTuc(idTheLoai, pCurrent);
                }
            }
        }

        void NapDanhSachTinTuc(int idTheLoai, int pCurrent)
        {
            var ls = BaiVietBLL.getListTheoTrang(idTheLoai, Hang.PAGESIZE, pCurrent, (int)TinhTrangBaiViet.DaDuyet);
            ddlTinTucSuKien.DataSource = ls;
            ddlTinTucSuKien.DataBind();
            var tsBai = BaiVietBLL.getToTal(idTheLoai, (int)TinhTrangBaiViet.DaDuyet);
            PhanTrang(tsBai, pCurrent, "../Modules/Index.aspx?mode=danhsachtintuc&idTL=" + idTheLoai.ToString() + "&");
        }

        int getPageCurrent()
        {
            var pageIndex = Request.QueryString["pageIndex"];
            int temp;
            int pCurrent = 1;
            if (!int.TryParse(pageIndex, out temp))
                pCurrent = 1;
            else
                pCurrent = int.Parse(pageIndex);
            return pCurrent;
        }

        void PhanTrang(int total, int pCurrent, string baseLink)
        {
            Paging paGing = new Paging(Hang.PAGESIZE);
            string resultPaging = paGing.CreatePaging(total, pCurrent, baseLink);
            if (string.IsNullOrEmpty(resultPaging))
                divPaging.Visible = false;
            else
            {
                divPaging.InnerHtml = resultPaging;
            }
        }

        
    }
}